package com.prs.crm.action.fs;

import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.prs.crm.action.FlowAction;
import com.prs.crm.annotation.Clew;
import com.prs.crm.annotation.FlowActive;
import com.prs.crm.annotation.FlowStart;
import com.prs.crm.context.Constrains;
import com.prs.crm.domain.fs.ApplyDeal;
import com.prs.crm.domain.fs.ApplyDetail;
import com.prs.crm.domain.fs.BankAccount;
import com.prs.crm.domain.fs.OutlayApply;
import com.prs.crm.domain.fs.OutlayItem;
import com.prs.crm.service.fs.ApplyDealService;
import com.prs.crm.service.fs.ApplyDetailService;
import com.prs.crm.service.fs.BankAccountService;
import com.prs.crm.service.fs.OutlayApplyService;
import com.prs.crm.service.fs.OutlayItemService;

public class OutlayApplyAction extends FlowAction<OutlayApply> {
	private OutlayApply outlayApply;
	private OutlayApplyService outlayApplyService;
	private PaginationSupport pagination;
	private Integer[] ids;

	private OutlayItemService outlayItemService;
	private Collection<OutlayItem> outlayItems;

	private ApplyDetail applyDetail;
	private ApplyDetailService applyDetailService;
	private Collection<ApplyDetail> applyDetails;

	private ApplyDeal applyDeal;
	private ApplyDealService applyDealService;
	private BankAccountService bankAccountService;
	private Collection<BankAccount> bankAccounts;

	private Double totalMoney;
	private File upload;
	private String uploadFileName;

	@FlowActive(target = "outlayApply", flowservice = "outlayApplyService")
	@Action("editOutlayApply")
	public String editOutlayApply() {
		outlayItems = this.getOutlayItemService().getAllOutlayItem();
		applyDetails = (Collection<ApplyDetail>) this.getStorage().get(
				"details");
		totalMoney = 0.0;
		if (applyDetails != null) {
			for (ApplyDetail detail : applyDetails) {
				totalMoney += detail.getMoney();
			}
		}
		return SUCCESS;
	}

	@Clew(results = { "success" })
	@FlowStart(target = "outlayApply", flowservice = "outlayApplyService")
	@Action(value = "submitOutlayApply", results = {
			@Result(name = "success", type = "redirect", location = "editOutlayApply"),
			@Result(name = "error", type = "chain", location = "editOutlayApply") })
	public String submitOutlayApply() {
		if (outlayApply.getTitle() == null
				|| "".equals(outlayApply.getTitle().trim())) {
			addActionError("主题不能为空");
			return ERROR;
		}
		if (outlayApply.getIsReturn() == null
				|| "".equals(outlayApply.getIsReturn().trim())) {
			addActionError("返还不能为空");
			return ERROR;
		}
		if (outlayApply.getLevel() == null) {
			addActionError("级别不能为空");
			return ERROR;
		}
		if (applyDetails == null) {
			addActionError("详细不能为空");
			return ERROR;
		}
		applyDetails = (Collection<ApplyDetail>) this.getStorage().get(
				"details");
		if (applyDetails == null) {
			this.addActionError("请添加费用明细");
			return ERROR;
		}
		totalMoney = 0.0;
		if (applyDetails != null) {
			for (ApplyDetail detail : applyDetails) {
				totalMoney += detail.getMoney();
			}
		}
		outlayApply.setMoney(totalMoney);
		this.getOutlayApplyService().submitOutlayApply(applyDetails,
				outlayApply, upload, uploadFileName);
		applyDetails.clear();
		this.setPrompt("提交费用申请成功");
		return SUCCESS;
	}

	@FlowActive(target = "outlayApply", flowservice = "outlayApplyService")
	@Action(value = "appendApplyDetail", results = {
			@Result(name = "success", type = "chain", location = "editOutlayApply"),
			@Result(name = "error", type = "chain", location = "editOutlayApply") })
	public String appendApplyDetail() {
		if (applyDetail == null || applyDetail.getMoney() == null) {
			this.addActionError("请输入正确的金额格式");
			return ERROR;
		}
		Collection<ApplyDetail> details = null;
		if (this.getStorage().get("details") == null) {
			details = new ArrayList<ApplyDetail>();
			this.getStorage().save("details", details);
		} else {
			details = (Collection<ApplyDetail>) this.getStorage()
					.get("details");
		}
		applyDetail.setId(details.size());
		details.add(applyDetail);

		return SUCCESS;
	}

	@FlowActive(target = "outlayApply", flowservice = "outlayApplyService")
	@Action(value = "removeApplyDetail", results = {
			@Result(name = "success", type = "chain", location = "editOutlayApply"),
			@Result(name = "error", type = "chain", location = "editOutlayApply") })
	public String removeApplyDetail() {
		if (ids == null) {
			this.addActionError("请选择要删除的明细");
			return ERROR;
		}
		Collection<ApplyDetail> details = (Collection<ApplyDetail>) this
				.getStorage().get("details");
		for (Integer id : ids) {
			Iterator<ApplyDetail> iter = details.iterator();
			while (iter.hasNext()) {
				ApplyDetail detial = iter.next();
				if (detial.getId().intValue() == id.intValue()) {
					details.remove(detial);
					break;
				}
			}
		}
		return SUCCESS;
	}

	@Action("listOutlayApply")
	public String listOutlayApply() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				OutlayApply.class);
		criteria.addOrder(Order.desc("recordTime"));
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getOutlayApplyService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}

	@Action("viewOutlayApply")
	public String viewOutlayApply() {
		if (outlayApply != null) {
			this.displayEndNodes(outlayApply);
		}
		return SUCCESS;
	}

	@Action("listWaitDealApply")
	public String listWaitDealApply() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				OutlayApply.class);
		criteria.add(Restrictions.eq("flowstatus",
				Constrains.WORKFLOWENTITY_STATE_VALID));
		criteria.add(Restrictions.isEmpty("applyDeals"));
		criteria.addOrder(Order.desc("recordTime"));
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getOutlayApplyService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}

	@Action("editApplyDeal")
	public String editApplyDeal() {
		bankAccounts = this.getBankAccountService().getAllBankAccount();
		return SUCCESS;
	}

	@Action(value = "submitApplyDeal", results = { @Result(name = "success", type = "redirect", location = "listWaitDealApply") })
	public String submitApplyDeal() {
		applyDeal.setApply(outlayApply);
		this.getApplyDealService().submitApplyDeal(applyDeal, upload,
				uploadFileName);
		return SUCCESS;
	}

	@Action("listApplyDeal")
	public String listDealApply() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				ApplyDeal.class);
		criteria.addOrder(Order.desc("recordTime"));
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getOutlayApplyService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}

	@Action("viewApplDeal")
	public String viewApplDeal() {
		return SUCCESS;
	}

	public OutlayApply getOutlayApply() {
		return outlayApply;
	}

	public void setOutlayApply(OutlayApply outlayApply) {
		this.outlayApply = outlayApply;
	}

	public OutlayApplyService getOutlayApplyService() {
		return outlayApplyService;
	}

	public void setOutlayApplyService(OutlayApplyService outlayApplyService) {
		this.outlayApplyService = outlayApplyService;
	}

	public PaginationSupport getPagination() {
		return pagination;
	}

	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}

	public Integer[] getIds() {
		return ids;
	}

	public void setIds(Integer[] ids) {
		this.ids = ids;
	}

	public File getUpload() {
		return upload;
	}

	public void setUpload(File upload) {
		this.upload = upload;
	}

	public String getUploadFileName() {
		return uploadFileName;
	}

	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}

	public OutlayItemService getOutlayItemService() {
		return outlayItemService;
	}

	public void setOutlayItemService(OutlayItemService outlayItemService) {
		this.outlayItemService = outlayItemService;
	}

	public Collection<OutlayItem> getOutlayItems() {
		return outlayItems;
	}

	public void setOutlayItems(Collection<OutlayItem> outlayItems) {
		this.outlayItems = outlayItems;
	}

	public void setApplyDetail(ApplyDetail applyDetail) {
		this.applyDetail = applyDetail;
	}

	public ApplyDetail getApplyDetail() {
		return applyDetail;
	}

	public BankAccountService getBankAccountService() {
		return bankAccountService;
	}

	public void setBankAccountService(BankAccountService bankAccountService) {
		this.bankAccountService = bankAccountService;
	}

	public Collection<BankAccount> getBankAccounts() {
		return bankAccounts;
	}

	public void setBankAccounts(Collection<BankAccount> bankAccounts) {
		this.bankAccounts = bankAccounts;
	}

	public void setApplyDetailService(ApplyDetailService applyDetailService) {
		this.applyDetailService = applyDetailService;
	}

	public ApplyDetailService getApplyDetailService() {
		return applyDetailService;
	}

	public void setApplyDetails(Collection<ApplyDetail> applyDetails) {
		this.applyDetails = applyDetails;
	}

	public ApplyDeal getApplyDeal() {
		return applyDeal;
	}

	public void setApplyDeal(ApplyDeal applyDeal) {
		this.applyDeal = applyDeal;
	}

	public ApplyDealService getApplyDealService() {
		return applyDealService;
	}

	public void setApplyDealService(ApplyDealService applyDealService) {
		this.applyDealService = applyDealService;
	}

	public Collection<ApplyDetail> getApplyDetails() {
		return applyDetails;
	}

	public void setTotalMoney(Double totalMoney) {
		this.totalMoney = totalMoney;
	}

	public Double getTotalMoney() {
		return totalMoney;
	}

}
